import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.options

from tornado.options import options, define
define('port', default=8000, help='run on given help', type=int)

class MainHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        cookies = self.get_secure_cookie('count')
        count = int(cookies) + 1 if cookies else 1

        countString = '1 time' if count == 1 else "%d times" % count

        self.set_secure_cookie('count', str(count))
        self.write(
            '<html><head><title>Cookie Counter</title></head>'
            '<body><h1>You’ve viewed this page %s times.</h1>' % countString +
            '</body></html>'
        )

if __name__ == '__main__':
    tornado.options.parse_command_line()
    setting = {
        "cookie_secret": "bZJc2sWbQLKos6GkHn/VB9oXwQt8S0R0kRvJ5/xJ89E="
    }

     application = tornado.web.Application([
        (r'/', MainHandler)
    ], **settings)

    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()